iT邦幫忙

2022 iThome 鐵人賽

DAY 25
0
Software Development

ClickHouse:時序資料庫建置與運行系列 第 25

day25-ClickHouse 客戶端連線介面方式(四)

  • 分享至 

  • xImage
  •  

前言

在本章節中,我們將會介紹如何使用與安裝ClickHouse官方所提供的C++資料庫客戶端連線函式庫。

建置需要的編譯環境

在使用C++客戶端程式範例之前,先要確保我們有相關編譯指令可以使用,相關的套件安裝方式以Ubuntu 18.04為例,執行指令與輸出的訊息如下:

eter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ sudo apt-get install build-essential git
Reading package lists... Done
.......
After this operation, 59.0 MB of additional disk space will be used.
Do you want to continue? [Y/n]
......
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.6) ...
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-odbc/build$ wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc 2>/dev/null | gpg --dearmor - | sudo tee /etc/apt/trusted.gpg.d/kitware.gpg >/dev/null
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-odbc/build$ sudo apt-add-repository "deb https://apt.kitware.com/ubuntu/ $(lsb_release -cs) main"
Get:1 http://mirrors.digitalocean.com/ubuntu bionic InRelease [242 kB]
Hit:2 http://mirrors.digitalocean.com/ubuntu bionic-updates InRelease
Hit:3 http://mirrors.digitalocean.com/ubuntu bionic-backports InRelease
Hit:4 http://deb.anydesk.com all InRelease
Hit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:6 https://packages.clickhouse.com/deb stable InRelease
Get:7 https://apt.kitware.com/ubuntu bionic InRelease [11.0 kB]
Get:8 https://apt.kitware.com/ubuntu bionic/main amd64 Packages [60.9 kB]
Fetched 314 kB in 2s (154 kB/s)
Reading package lists... Done
......
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-odbc/build$ sudo apt-get install cmake
......
Setting up cmake-data (3.24.1-0kitware1ubuntu18.04.1) ...
Setting up cmake (3.24.1-0kitware1ubuntu18.04.1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-odbc/build$

C++函式庫介紹

ClickHouse官方提供的C++函式庫是在:https://github.com/ClickHouse/clickhouse-cpp,我們先使用git clone指令將此專案複製回來到本地端要運行的機器上,相關執行指令與輸出訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ git clone https://github.com/ClickHouse/clickhouse-cpp
Cloning into 'clickhouse-cpp'...
remote: Enumerating objects: 4149, done.
remote: Counting objects: 100% (1704/1704), done.
remote: Compressing objects: 100% (258/258), done.
remote: Total 4149 (delta 1575), reused 1464 (delta 1446), pack-reused 2445
Receiving objects: 100% (4149/4149), 1.35 MiB | 2.71 MiB/s, done.
Resolving deltas: 100% (3006/3006), done.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$

C++函式庫安裝(以Ubuntu發行版為例)

接著切換到clickhouse-cpp目錄,並切換與建立build目錄,並執行cmake之指令,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ cd clickhouse-cpp/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp$ mkdir build
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp$ cd build/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/peter/clickhouse-cpp/build
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$

當完成上述的步驟之後,會產生相對應的Makefile,我們就可以進行編譯了,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$ make
[  1%] Building C object contrib/lz4/CMakeFiles/lz4-lib.dir/lz4.c.o
[  3%] Building C object contrib/lz4/CMakeFiles/lz4-lib.dir/lz4hc.c.o
......
[100%] Linking CXX static library libclickhouse-cpp-lib-static.a
[100%] Built target clickhouse-cpp-lib-static
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$

從上述的編譯步驟可以知道,我們已經編譯完成ClickHouse之C++函式庫了,若要將相關的動態連結與標頭檔安裝到系統層級的函式庫目錄,則可以使用下列的指令完成:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$ sudo make install
[sudo] password for peter:
Consolidate compiler generated dependencies of target lz4-lib
[  4%] Built target lz4-lib
Consolidate compiler generated dependencies of target absl-lib
[  8%] Built target absl-lib
Consolidate compiler generated dependencies of target cityhash-lib
[ 11%] Built target cityhash-lib
Consolidate compiler generated dependencies of target clickhouse-cpp-lib
[ 55%] Built target clickhouse-cpp-lib
Consolidate compiler generated dependencies of target clickhouse-cpp-lib-static
[100%] Built target clickhouse-cpp-lib-static
Install the project...
......
-- Installing: /usr/local/include/clickhouse/types/type_parser.h
-- Installing: /usr/local/include/clickhouse/types/types.h
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp/build$

因為此C++函式庫需要使用Abseil之C++函式庫,因此我們同時需要編譯此函式庫,下列是編譯此函式庫的方式,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ git clone https://github.com/abseil/abseil-cpp
Cloning into 'abseil-cpp'...
remote: Enumerating objects: 18586, done.
remote: Counting objects: 100% (876/876), done.
remote: Compressing objects: 100% (526/526), done.
remote: Total 18586 (delta 359), reused 369 (delta 350), pack-reused 17710
Receiving objects: 100% (18586/18586), 11.38 MiB | 17.47 MiB/s, done.
Resolving deltas: 100% (14512/14512), done.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ cd abseil-cpp/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp$ mkdir build
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp$ cd build/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp/build$ cmake ..
-- The CXX compiler identification is GNU 7.5.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at CMakeLists.txt:72 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17
-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17 - Failed
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
CMake Warning at CMakeLists.txt:189 (message):
    The default and system-level install directories are unsupported except in LTS   releases of Abseil.  Please set CMAKE_INSTALL_PREFIX to install Abseil in your   source or build tree directly.


-- Configuring done
-- Generating done
-- Build files have been written to: /home/peter/abseil-cpp/build

從上述的執行指令與輸出的訊息可以知道,先將此專案複製回來,接著切換到該專案目錄底下,建立並切換到build目錄,以及執行cmake ..之指令產生編譯需要Makefile

產生完成Makefile檔案之後,我們可以使用make指令進行編譯,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp/build$ make
[  0%] Building CXX object absl/base/CMakeFiles/log_severity.dir/log_severity.cc.o
[  1%] Linking CXX static library libabsl_log_severity.a
[  1%] Built target log_severity
[  1%] Building CXX object absl/base/CMakeFiles/raw_logging_internal.dir/internal/raw_logging.cc.o
[  2%] Linking CXX static library libabsl_raw_logging_internal.a
......
[100%] Building CXX object absl/types/CMakeFiles/bad_any_cast_impl.dir/bad_any_cast.cc.o
[100%] Linking CXX static library libabsl_bad_any_cast_impl.a
[100%] Built target bad_any_cast_impl
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp/build$

從上述的輸出訊息可以知道,我們完成了Abseil函式庫的編譯了,若要將此C++函式庫之標頭檔與動態連結函式庫安裝到系統層級的函式庫目錄,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp/build$ sudo make install
Consolidate compiler generated dependencies of target log_severity
[  1%] Built target log_severity
Consolidate compiler generated dependencies of target raw_logging_internal
[  2%] Built target raw_logging_internal
Consolidate compiler generated dependencies of target spinlock_wait
[  3%] Built target spinlock_wait
Consolidate compiler generated dependencies of target base
[  6%] Built target base
Consolidate compiler generated dependencies of target malloc_internal
[  7%] Built target malloc_internal
Consolidate compiler generated dependencies of target throw_delegate
......
-- Installing: /usr/local/lib/pkgconfig/absl_utility.pc
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/abseil-cpp/build$

這樣就完成該C++資料庫客戶端函式庫的安裝了。

C++函式庫範例

安裝好C++函式庫之後,我們可以使用下列的範例專案進行C++函式庫的應用與執行,相關執行指令與輸出的訊息如下:

peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ git clone https://github.com/peter279k/clickhouse-cpp-example
Cloning into 'clickhouse-cpp-example'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 12 (delta 3), pack-reused 0
Unpacking objects: 100% (12/12), done.
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~$ cd clickhouse-cpp-example/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$ ls
CMakeLists.txt  README.md  clickhouse.cpp  clickhouse_error_example.cpp
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$ vim clickhouse.cpp
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$ cat clickhouse.cpp
#include <iostream>
#include <clickhouse/client.h>


using namespace clickhouse;

int main() {

    // 初始化客戶端連線
    Client client(clickhouse::ClientOptions().SetHost("localhost").SetUser("default").SetPassword("password").SetDefaultDatabase("default"));

    // 刪除資料表
    client.Execute("DROP TABLE IF EXISTS default.numbers");

    // 建立資料表
    client.Execute("CREATE TABLE IF NOT EXISTS default.numbers (id UInt64, name String) ENGINE = Memory");

    // 寫入資料到上述的資料表
    {
        Block block;

        auto id = std::make_shared<ColumnUInt64>();
        id->Append(1);
        id->Append(7);

        auto name = std::make_shared<ColumnString>();
        name->Append("one");
        name->Append("seven");

        block.AppendColumn("id"  , id);
        block.AppendColumn("name", name);

        client.Insert("default.numbers", block);
    }

    // 篩選上一步寫入資料表的資料
    client.Select("SELECT id, name FROM default.numbers", [] (const Block& block)
        {
            for (size_t i = 0; i < block.GetRowCount(); ++i) {
                std::cout << block[0]->As<ColumnUInt64>()->At(i) << " "
                          << block[1]->As<ColumnString>()->At(i) << "\n";
            }
        }
    );

    // 刪除資料表
    client.Execute("DROP TABLE default.numbers");

    return 0;
}
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$ mkdir build/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example$ cd build/
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/peter/clickhouse-cpp-example/build
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example/build$ make
[ 50%] Building CXX object CMakeFiles/clickhouse-example.dir/clickhouse.cpp.o
[100%] Linking CXX executable clickhouse-example
[100%] Built target clickhouse-example
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example/build$ ./clickhouse-example
1 one
7 seven
peter@ubuntu-s-4vcpu-8gb-amd-sgp1-01:~/clickhouse-cpp-example/build$

從上述的輸出出訊息可以得知如下的步驟:

  • 首先先用git clone指令方式將clickhouse-cpp-example專案複製回來。
  • 使用vim編輯器將clickhouse.cpp檔案中password的改成確切的ClickHouse資料庫密碼。
  • 改完上述的檔案之後,接著使用cat指令將程式碼內容印出來確認修改的資料庫使用者密碼是正確的。
  • 接著切換到上述的專案目錄,之後建立並切換到build目錄。
  • 執行cmake ..指令進行編譯指令設定與產生出對應的`Makefile檔案。
  • 執行make指令進行編譯clickhouse.cpp程式並輸出clickhouse-example之可執行二進位元檔。
  • 執行./clickhouse-example之程式,就可以看到運行的結果了。

在上述的clickhouse-cpp-example專案中,還有一個檔案叫做clickhouse_error_example.cpp,這個是從clickhouse-cpp專案中的README.md擷取的範例程式碼。

這個範例程式碼在編譯時候是有問題的,只是在這個專案做個記錄,因此筆者在這邊則是自己建立範例的C++專案,而不是直接使用clickhouse-cpp專案中所提供的範例程式碼。

C++函式庫安裝(以Fedora發行版為例)

這邊以Fedora 35版本為例,首先先準備一個Fedora 35版本的作業系統並安裝好ClickHouse資料庫,安裝的方法可以參考前面的章節,接著執行指令與輸出的訊息如下:

[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# yum update
Last metadata expiration check: 0:00:02 ago on Wed 05 Oct 2022 04:21:08 AM UTC.
......
(199/199): zram-generator-defaults-1.1.2-1.fc35.noarch.rpm                  207 kB/s | 9.7 kB     00:00
------------------------------------------------------------------------------------------------------------
Total                                                                        29 MB/s | 345 MB     00:11
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
......
 pcsc-lite-libs-1.9.5-1.fc35.x86_64                  polkit-0.120-1.fc35.2.x86_64
  polkit-libs-0.120-1.fc35.2.x86_64                   polkit-pkla-compat-0.1-20.fc35.x86_64
  unbound-anchor-1.16.2-3.fc35.x86_64                 vim-data-2:9.0.475-1.fc35.noarch

Complete!
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# yum grouinstall "Development Tools"
No such command: grouinstall. Please use /usr/bin/yum --help
It could be a YUM plugin command, try: "yum install 'dnf-command(grouinstall)'"
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# yum groupinstall "Development Tools"
Last metadata expiration check: 0:02:16 ago on Wed 05 Oct 2022 04:39:12 AM UTC.
Dependencies resolved.
============================================================================================================
 Package                                    Architecture Version                        Repository     Size
============================================================================================================
Installing group/module packages:
 diffstat                                   x86_64       1.64-5.fc35                    fedora         42 k
 doxygen                                    x86_64       1:1.9.1-12.fc35                updates       4.7 M
 git                                        x86_64       2.37.3-1.fc35                  updates        65 k
 patch                                      x86_64       2.7.6-15.fc35                  fedora        127 k
 patchutils                                 x86_64       0.4.2-6.fc35                   fedora        100 k
 subversion                                 x86_64       1.14.2-5.fc35                  updates       1.0 M
 systemtap
 ......
 Transaction Summary
============================================================================================================
Install  305 Packages

Total download size: 252 M
Installed size: 971 M
Is this ok [y/N]: y
......
  zlib-devel-1.2.11-32.fc35.x86_64

Complete!
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# yum install -y cmake
......
Complete!
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# yum install -y gcc-c++
Last metadata expiration check: 0:00:06 ago on Wed 05 Oct 2022 05:06:03 AM UTC.
Dependencies resolved.
.......
Installed:
  gcc-c++-11.3.1-3.fc35.x86_64                     libstdc++-devel-11.3.1-3.fc35.x86_64

Complete!
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]#

從上述執行指令與輸出的訊息可以得知,我們已經完成了編譯環境的建置,接下來就是函式庫的安裝,相關執行指令與輸出的訊息如下:

[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/clickhouse/clickhouse-cpp
Cloning into 'clickhouse-cpp'...
remote: Enumerating objects: 4149, done.
remote: Counting objects: 100% (1746/1746), done.
remote: Compressing objects: 100% (258/258), done.
remote: Total 4149 (delta 1615), reused 1506 (delta 1488), pack-reused 2403
Receiving objects: 100% (4149/4149), 1.35 MiB | 18.15 MiB/s, done.
Resolving deltas: 100% (3008/3008), done.
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# cd clickhouse-cpp/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp]# mkdir build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp]# cd build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The CXX compiler identification is GNU 11.3.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/clickhouse-cpp/build
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# make
[  1%] Building C object contrib/lz4/CMakeFiles/lz4-lib.dir/lz4.c.o
......
[100%] Linking CXX static library libclickhouse-cpp-lib-static.a
[100%] Built target clickhouse-cpp-lib-static
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# make install
......
-- Installing: /usr/local/include/clickhouse/types/types.h
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]#
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# cd
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/abseil/abseil-cpp
Cloning into 'abseil-cpp'...
remote: Enumerating objects: 18586, done.
remote: Counting objects: 100% (1583/1583), done.
remote: Compressing objects: 100% (543/543), done.
remote: Total 18586 (delta 1057), reused 1060 (delta 1040), pack-reused 17003
Receiving objects: 100% (18586/18586), 11.43 MiB | 5.41 MiB/s, done.
Resolving deltas: 100% (14521/14521), done.
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# cd abseil-cpp/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 abseil-cpp]# mkdir build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 abseil-cpp]# cd build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The CXX compiler identification is GNU 11.3.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at CMakeLists.txt:72 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17
-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17 - Success
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
CMake Warning at CMakeLists.txt:189 (message):
    The default and system-level install directories are unsupported except in LTS   releases of Abseil.  Please set CMAKE_INSTALL_PREFIX to install Abseil in your   source or build tree directly.


-- Configuring done
-- Generating done
-- Build files have been written to: /root/abseil-cpp/build
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# make
[  0%] Building CXX object absl/base/CMakeFiles/log_severity.dir/log_severity.cc.o
[  1%] Linking CXX static library libabsl_log_severity.a
[  1%] Built target log_severity
[  1%] Building CXX object absl/base/CMakeFiles/raw_logging_internal.dir/internal/raw_logging.cc.o
.......
[100%] Built target cordz_sample_token
[100%] Building CXX object absl/types/CMakeFiles/bad_any_cast_impl.dir/bad_any_cast.cc.o
[100%] Linking CXX static library libabsl_bad_any_cast_impl.a
[100%] Built target bad_any_cast_impl
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# make install
......
-- Installing: /usr/local/lib64/pkgconfig/absl_compare.pc
-- Installing: /usr/local/lib64/pkgconfig/absl_utility.pc
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]#

從上述執行的指令可以知道,我們已經安裝好了ClickHouse資料庫的C++客戶端以及相依的Abseil函式庫了,接著我們可以使用範例程式碼進行相關的執行與操作,相關執行指令與輸出的訊息如下:

[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/peter279k/clickhouse-cpp-example
Cloning into 'clickhouse-cpp-example'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 12 (delta 3), pack-reused 0
Receiving objects: 100% (12/12), done.
Resolving deltas: 100% (3/3), done.
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 ~]# cd clickhouse-cpp-example/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# yum install -y vim
.......
Installed:
  gpm-libs-1.20.7-28.fc35.x86_64        libsodium-1.0.18-8.fc35.x86_64  vim-common-2:9.0.475-1.fc35.x86_64
  vim-enhanced-2:9.0.475-1.fc35.x86_64

Complete!
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# vim clickhouse.cpp
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# mkdir build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# cd build/
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The C compiler identification is GNU 11.3.1
-- The CXX compiler identification is GNU 11.3.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/clickhouse-cpp-example/build
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# make
[ 50%] Building CXX object CMakeFiles/clickhouse-example.dir/clickhouse.cpp.o
[100%] Linking CXX executable clickhouse-example
[100%] Built target clickhouse-example
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]# ./clickhouse-example
1 one
7 seven
[root@fedora-s-4vcpu-8gb-amd-sgp1-01 build]#

從上述執行指令所輸出的訊息可以知道,複製範例程式碼之後,接著切換專案目錄進去,接著這邊以vim文字編輯器修改資料庫使用者密碼,接著建立與切換build目錄後,執行cmake ..make指令分別進行設定與產生Makefile,以及編譯指定的C++程式,最後就會輸出該範例程式得到的結果了。

C++函式庫安裝(以Rocky Linux發行版為例)

這邊以Rocky Linux 9為例,首先先準備一個Rocky Linux 9版本的作業系統並安裝好ClickHouse資料庫,安裝資料庫的方法可以參考先前的章節,接著執行下列的指令與輸出的訊息如下:

[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# yum update
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:00:36 ago on Wed Oct  5 06:35:16 2022.
Dependencies resolved.
......
Install   4 Packages
Upgrade  38 Packages

Total download size: 89 M
Is this ok [y/N]: y
......
Installed:
  kernel-5.14.0-70.26.1.el9_0.x86_64                     kernel-core-5.14.0-70.26.1.el9_0.x86_64
  kernel-modules-5.14.0-70.26.1.el9_0.x86_64             openldap-compat-2.6.2-1.el9_0.x86_64

Complete!
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# yum groupinstall "Development Tools"
......
Transaction Summary
============================================================================================================
Install  314 Packages

Total download size: 262 M
Installed size: 962 M
Is this ok [y/N]: y
  zlib-devel-1.2.11-31.el9_0.1.x86_64

Complete!
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# yum install -y gcc-c++ cmake
Failed to set locale, defaulting to C.UTF-8
Last metadata expiration check: 0:03:41 ago on Wed Oct  5 07:03:55 2022.
Package gcc-c++-11.2.1-9.4.el9.x86_64 is already installed.
......
Installed:
  cmake-3.20.2-7.el9.x86_64                         cmake-data-3.20.2-7.el9.noarch
  cmake-filesystem-3.20.2-7.el9.x86_64              cmake-rpm-macros-3.20.2-7.el9.noarch
  libuv-1:1.42.0-1.el9.x86_64                       vim-filesystem-2:8.2.2637-16.el9_0.3.noarch

Complete!
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/abseil/abseil-cpp
Cloning into 'abseil-cpp'...
remote: Enumerating objects: 18586, done.
remote: Counting objects: 100% (1583/1583), done.
remote: Compressing objects: 100% (543/543), done.
remote: Total 18586 (delta 1057), reused 1060 (delta 1040), pack-reused 17003
Receiving objects: 100% (18586/18586), 11.43 MiB | 8.99 MiB/s, done.
Resolving deltas: 100% (14521/14521), done.
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# cd abseil-cpp/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 abseil-cpp]# mkdir build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 abseil-cpp]# cd build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The CXX compiler identification is GNU 11.2.1
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at CMakeLists.txt:72 (message):
  A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
  3.8 and up.  We recommend enabling this option to ensure your project still
  builds correctly.


-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17
-- Performing Test ABSL_INTERNAL_AT_LEAST_CXX17 - Success
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE
CMake Warning at CMakeLists.txt:189 (message):
    The default and system-level install directories are unsupported except in LTS   releases of Abseil.  Please set CMAKE_INSTALL_PREFIX to install Abseil in your   source or build tree directly.


-- Configuring done
-- Generating done
-- Build files have been written to: /root/abseil-cpp/build
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# make
[  0%] Building CXX object absl/base/CMakeFiles/strerror.dir/internal/strerror.cc.o
[  0%] Linking CXX static library libabsl_strerror.a
[  0%] Built target strerror
[  0%] Building CXX object absl/base/CMakeFiles/log_severity.dir/log_severity.cc.o
[  1%] Linking CXX static library libabsl_log_severity.a
.......
[100%] Building CXX object absl/types/CMakeFiles/bad_any_cast_impl.dir/bad_any_cast.cc.o
[100%] Linking CXX static library libabsl_bad_any_cast_impl.a
[100%] Built target bad_any_cast_impl
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# make install
......
-- Installing: /usr/local/lib64/libabsl_bad_variant_access.a
-- Installing: /usr/local/lib64/pkgconfig/absl_variant.pc
-- Installing: /usr/local/lib64/pkgconfig/absl_compare.pc
-- Installing: /usr/local/lib64/pkgconfig/absl_utility.pc
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# cd
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/ClickHouse/clickhouse-cpp
Cloning into 'clickhouse-cpp'...
remote: Enumerating objects: 4149, done.
remote: Counting objects: 100% (1704/1704), done.
remote: Compressing objects: 100% (258/258), done.
remote: Total 4149 (delta 1575), reused 1464 (delta 1446), pack-reused 2445
Receiving objects: 100% (4149/4149), 1.35 MiB | 18.40 MiB/s, done.
Resolving deltas: 100% (3006/3006), done.
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# cd clickhouse-cpp/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp]# mkdir build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp]# cd build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The C compiler identification is GNU 11.2.1
-- The CXX compiler identification is GNU 11.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/clickhouse-cpp/build
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# make
[  1%] Building C object contrib/lz4/CMakeFiles/lz4-lib.dir/lz4.c.o
......
[100%] Linking CXX shared library libclickhouse-cpp-lib.so
[100%] Built target clickhouse-cpp-lib
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# make install
......
-- Installing: /usr/local/include/clickhouse/columns/uuid.h
-- Installing: /usr/local/include/clickhouse/types/type_parser.h
-- Installing: /usr/local/include/clickhouse/types/types.h
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# cd
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# git clone https://github.com/peter279k/clickhouse-cpp-example
Cloning into 'clickhouse-cpp-example'...
remote: Enumerating objects: 12, done.
remote: Counting objects: 100% (12/12), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 12 (delta 3), reused 12 (delta 3), pack-reused 0
Receiving objects: 100% (12/12), done.
Resolving deltas: 100% (3/3), done.
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 ~]# cd clickhouse-cpp-example/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# yum install -y vim
......
Installed:
  gpm-libs-1.20.7-29.el9.x86_64                         vim-common-2:8.2.2637-16.el9_0.3.x86_64
  vim-enhanced-2:8.2.2637-16.el9_0.3.x86_64

Complete!
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]#
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# vim clickhouse.cpp
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# mkdir build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 clickhouse-cpp-example]# cd build/
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# cmake ..
-- The C compiler identification is GNU 11.2.1
-- The CXX compiler identification is GNU 11.2.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/clickhouse-cpp-example/build
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# make
[ 50%] Building CXX object CMakeFiles/clickhouse-example.dir/clickhouse.cpp.o
[100%] Linking CXX executable clickhouse-example
[100%] Built target clickhouse-example
[root@rockylinux-s-4vcpu-8gb-amd-sgp1-01 build]# ./clickhouse-example
1 one
7 seven

從上述的輸出訊息可以知道,首先安裝編譯需要的套件,接著複製Abseil專案並進行設定與編譯函式庫,接著複製ClickHouse之C++函式庫專案進行設定與編譯函式庫。

上述的函式庫都編譯完成之後,最後再複製clickhouse-cpp-example專案,將clickhouse.cpp程式進行編譯與執行,得到結果就會是上述的輸出訊息。

C++函式庫使用故障與排除

若在編譯的時候,輸出下列的錯誤訊息:

clickhouse/types/types.h:3:10: fatal error: absl/numeric/int128.h: No such file or directory 3 | #include "absl/numeric/int128.h"

則在編譯的時候,沒有找到有關於Abseil之C++函式庫之標頭檔,可以參考上面的章節將此C++函式庫進行編譯以及安裝標頭檔和動態連結函式庫到系統層級的目錄中。

結論

從本次章節中,我們可以知道如何使用ClickHouse官方提供的C++資料庫客戶端函式庫進行設定、編譯與安裝到當前的作業系統,並示範在不同的Linux發行版本上的運行與建置方式。

在下一章節中,將會介紹其他ClickHouse資料庫官方客戶端與第三方的客戶端函式庫範例與應用。

參考資料


上一篇
day24-ClickHouse 客戶端連線介面方式(三)
下一篇
day26-ClickHouse 客戶端連線介面方式(五)
系列文
ClickHouse:時序資料庫建置與運行30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言